// Pulsar status.net HUD 
// 
// This scripts allow and object receive a message from a specified channel 
// and post to a microblloging site based on status.net 
//
// @author Marlon J. Manrique 
// @version 0.2 (29 June 2010)  
//
// License   
// http://creativecommons.org/licenses/GPL/2.0/
//
// $Id

// Status.net account information 
string psnh_username;
string psnh_password;

// Channel where the object expect the message 
integer psnh_channel = 5;

// Status.net server information
string psnh_serverName;
string psnh_serverPath;
string psnh_restPath;

// Configuration filename 
integer psnh_configutarionLineCounter;
string psnh_configutarionFilename = "PulsarStatusNetHUD.conf";
key psnh_configutarionLineId;

// Http Request identifier 
key psnh_requestID;

// Load the configuration of the script from a notecard 
PSNH_loadConfiguration()
{
	// Check if the configuration file exists 
	if(llGetInventoryType(psnh_configutarionFilename) != INVENTORY_NOTECARD) {
		llOwnerSay("Missing Configuration File : " + psnh_configutarionFilename);
		return;
	}
	
	// Init line counter 
	psnh_configutarionLineCounter = 0;
	
	// Read the first line 
	psnh_configutarionLineId = llGetNotecardLine(
		psnh_configutarionFilename, psnh_configutarionLineCounter++);
}

// Read the next line of configuration data 
// @param data Line of the configuration file 
PSNH_readConfiguration(string data)
{
	// Check if is the end of the file 
	if(data == EOF) {
		llOwnerSay("Configuration Loaded");
		return;
	}
	
	// If the line is not empty 	
	if(data != "") {
		// If is no a commentary (begins with #)
		if(llSubStringIndex(data,"#") != 0) {
			// Get the index of = 
			integer index = llSubStringIndex(data, "=");
			
			// If the line has = 
			if(index != -1) {
				// Get name and value 
				string name = llGetSubString(data, 0, index-1);
				string value = llGetSubString(data, index+1, -1);
				
				// If line value is empty 
				if(index+1 == llStringLength(data))
					value = "";
				
				// Trim value 
				value = llDumpList2String(llParseString2List(value, [" "], []), " ");
				
				// Init values 
				if(name == "psnh_username")
					psnh_username = value;
				else if(name == "psnh_password")
					psnh_password = value;
				else if(name == "psnh_channel")
					psnh_channel = (integer) value;
				else if(name == "psnh_serverName")
					psnh_serverName = value;
				else if(name == "psnh_serverPath")
					psnh_serverPath = value;
				else if(name == "psnh_restPath")
					psnh_restPath = value;
			}
		}
	}
	
	// Read the next line 
	psnh_configutarionLineId = llGetNotecardLine(
		psnh_configutarionFilename, psnh_configutarionLineCounter++);
}

// Star listening in the channel specified in the configuration 
PSNH_startListening()
{
   	// Listen all the messages from the owner 
	llListen(psnh_channel, "", llGetOwner(), "");
	// Show a message 
	llOwnerSay("Ready!!");
} 

// Update the estatus of the user in the microblogging site 
// @param message The message to use like status 
PSNH_updateStatus(string message) {
	// Construct the url to send the message
	// Something like this  
	// http://username:password@server.com/api/statuses/update.xml
	string url = "http://" + psnh_username + ":" + psnh_password + "@" 
		+ psnh_serverName + psnh_serverPath + psnh_restPath;
		
	// Prepare request values, using post and send the message encoded  
	string body = "status=" + message;
	list parameters = [ 
		HTTP_METHOD, "POST",
		HTTP_MIMETYPE, "application/x-www-form-urlencoded; charset=UTF-8"
	];

	// Sen the request 
	psnh_requestID = llHTTPRequest(url, parameters, body); 
}

// Check the reponse send by the server 
// @param request_id The identifier of the request 
// @param status The http reponse code send by the server
// @param metadata Metadata associated with the request 
// @param body The response information send by the server
PSNH_checkResponse(key request_id, integer status, list metadata, string body) {
	// If any problem with the server, show the page that send
	// The server response 200 if not problem      	
	if(status != 200)
		llOwnerSay(body);
	// Show a info message 
	else
		llOwnerSay("Status Update!!!");	
}

// Default state that ways for a message in the channel specified 
// and send the message like the status to the microblogging site 
default {
	
	// Load the configuration before start listening messages  
    state_entry() {
    	// Load the configuration 
    	PSNH_loadConfiguration();
    }
    
    // Load the configuration before start listening messages  
    on_rez(integer start_param) {
    	// Load the configuration 
    	PSNH_loadConfiguration();    	
    }

    // Read data from the configuration notecard 
    dataserver(key queryid, string data) {
    	// Check if the request is load data from configuration notecard 
    	if(queryid == psnh_configutarionLineId) {
    		PSNH_readConfiguration(data);
    		// If end of file, start listening 
    		if(data == EOF)
    			PSNH_startListening();	
    	}
    }    
    
    // Listen all the messages and post to the microblogging site 
    listen(integer channel, string name, key id, string message) {
    	// Update the status using the message give by the user
    	PSNH_updateStatus(message);
    }
    
    // Response send by the microblogging site 
    http_response(key request_id, integer status, list metadata, string body) {
    	PSNH_checkResponse(request_id, status, metadata, body);
    }
}